package com.atzongmaotan.nio;

import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.channels.ServerSocketChannel;

/**
 * ClassName:    BasicBuffer
 * Package:  com.atzongmaotan
 * Description:
 *
 * @Author: interface
 * @Create: 2024/9/29 - 下午4:39
 * @Version: v1.0
 */
public class BasicBuffer {
    public static void main(String[] args) {
        createOnlyReadBuffer();
    }
    private static void BufferDate() {
        //分配空间
        ByteBuffer byteBuffer = ByteBuffer.allocate(64);
        //类型化方式放入数据
        byteBuffer.putInt(100);
        byteBuffer.putLong(9L);
        byteBuffer.putChar('学');
        byteBuffer.putShort((short)10);
        //切换模式
        byteBuffer.flip();
        System.out.println(byteBuffer.getInt());
        System.out.println(byteBuffer.getLong());
        System.out.println(byteBuffer.getChar());
        System.out.println(byteBuffer.getShort());
    }
    private static void crateBuffer() {
        //创建一个桶存放数据
        IntBuffer allocate = IntBuffer.allocate(5);
        for (int i = 0; i < allocate.capacity(); i++) {
            allocate.put(i*2);
        }
        //切换到write mode
        allocate.flip();
        while (allocate.hasRemaining()) {
            System.out.println(allocate.get());
        }
    }
    public static void createOnlyReadBuffer(){
        ByteBuffer byteBuffer = ByteBuffer.allocate(64);
        for (int i = 0; i < 64; i++) {
            byteBuffer.put((byte)i);
        }
        //切换模式
        byteBuffer.flip();
        ByteBuffer readOnlyBuffer = byteBuffer.asReadOnlyBuffer();
        while (readOnlyBuffer.hasRemaining()) {
            System.out.println(readOnlyBuffer.get());
        }
       // readOnlyBuffer.put((byte)65); java.nio.ReadOnlyBufferException
    }
}
